package com.lh.runmanage.controller;

import com.lh.runmanage.dto.MenuAuthorityJ6Dto;
import com.lh.runmanage.model.*;
import com.lh.runmanage.model.InPutParam.*;
import com.lh.runmanage.service.ColService;
import com.lh.runmanage.service.MenuAuthorityService;
import com.lh.runmanage.service.ProjectAndProductService;
import com.lh.runmanage.service.ProjectService;
import com.lh.runmanage.unit.GetPropertiesClass;
import com.lh.runmanage.unit.LhClass;
import com.lh.runmanage.unit.ResultStruct;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：梁昊
 * @create 2021-07-21 14:43
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/menuAuthorityController")
@Api(value = "加权平均值控制层", description = "系统运行态势平台展示端")
public class MenuAuthorityController {
    @Autowired
    MenuAuthorityService menuAuthorityService;
    @Autowired
    GetPropertiesClass getPropertiesClass;
    @Autowired
    ProjectService projectService;
    @Autowired
    ProjectAndProductService projectAndProductService;
    @Autowired
    ColService colService;

    /**
     * 展示端方法：T2，方法ID：SE202107211437309166DB6EF0977D2
     *
     * @return 界面加权平均值
     */
    @ApiOperation(value = "展示端方法：T2[界面加权平均值],方法ID：SE202107211437309166DB6EF0977D5", notes = "/menuAuthorityController/V1/selectMenuAuthorityOrderByT2")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "beginDate", value = "数据生成时间", dataType = "Date", required = true)
            , @ApiImplicitParam(name = "endDate", value = "数据生成时间", dataType = "Date", required = true)
    })
    @PostMapping("/V1/selectMenuAuthorityOrderByT2")
    public ResultVO selectMenuAuthorityOrderByT2(@RequestParam(value = "beginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate
            , @RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        final List<ProjectD4Model> idOrderBy = getProjectByTime(beginDate, endDate);
        if (idOrderBy.size() > 0) {
            List<String> projectIds = new ArrayList<>(idOrderBy.size());
            for (ProjectD4Model item :
                    idOrderBy) {
                projectIds.add(item.getId());
            }
            MenuAuthorityInParam menuAuthorityInParam = new MenuAuthorityInParam();
            menuAuthorityInParam.setCreateTime(beginDate);
            menuAuthorityInParam.setId(projectIds);
            return ResultStruct.success(menuAuthorityService.selectMenuAuthorityOrderBy(menuAuthorityInParam));
        } else
            return ResultStruct.success(null);
    }

    private List<ProjectD4Model> getProjectByTime(Date beginDate, Date endDate) {
        ProjAvgD8InParam projAvgD8InParam = new ProjAvgD8InParam();
        projAvgD8InParam.setBeginDate(beginDate);
        projAvgD8InParam.setEndDate(endDate);
        return projectService.selectProjectT2OrderTime(projAvgD8InParam);
    }

    /**
     * 展示端方法：D7，方法ID：SE202107211437309166DB6EF0977D1
     *
     * @return 界面加权平均值
     */
    @ApiOperation(value = "展示端方法：D7[界面加权平均值],方法ID：SE202107211437309166DB6EF0977D1", notes = "/menuAuthorityController/V1/selectMenuAuthorityOrderByD7")
    @PostMapping("/V1/selectMenuAuthorityOrderByD7")
    public ResultVO selectMenuAuthorityOrderByD7() {
        final List<String> list = get20IdOrderBy();
        return selectMenuAuthority(list, null);
    }

    /**
     * 展示端方法：P4，方法ID：SE202107211437309166DB6EF0977D2
     *
     * @return 界面加权平均值
     */
    @ApiOperation(value = "展示端方法：P4[界面加权平均值],方法ID：SE202107211437309166DB6EF0977D2", notes = "/menuAuthorityController/V1/selectMenuAuthorityOrderByD7")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品ID", dataType = "String", required = true)
    })
    @PostMapping("/V1/selectMenuAuthorityOrderByP4")
    public ResultVO selectMenuAuthorityOrderByP4(@RequestParam(value = "productId") String productId) {
        final List<String> list = projectAndProductService.selectProjectListByProductId(productId);
        return selectMenuAuthority(list, productId);
    }

    private ResultVO selectMenuAuthority(List<String> list, String productId) {
        if (!list.isEmpty()) {
            final Date defaultDate = LhClass.getDefaultDate();
            MenuAuthorityInParam menuAuthorityInParam = new MenuAuthorityInParam();
            menuAuthorityInParam.setCreateTime(defaultDate);
            menuAuthorityInParam.setId(list);
            menuAuthorityInParam.setProductCode(productId);
            return ResultStruct.success(menuAuthorityService.selectMenuAuthorityGroupBy(menuAuthorityInParam));
        } else
            return ResultStruct.success(null);
    }

    /**
     * 展示端方法：P3，方法ID：SE202107211437309166DB6EF0977D3
     *
     * @return 界面加权平均值
     */
    @ApiOperation(value = "展示端方法：P3[关联项目热力图],方法ID：SE202107211437309166DB6EF0977D3", notes = "/menuAuthorityController/V1/selectMenuAuthorityOrderByP3")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "createTime", value = "数据生成时间", dataType = "Date", required = true)
    })
    @PostMapping("/V1/selectMenuAuthorityOrderByP3")
    public ResultVO selectMenuAuthorityOrderByP3(@RequestParam(value = "productId") String productId
            , @RequestParam(value = "createTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime) {
        final List<String> list = projectAndProductService.selectProjectListByProductId(productId);
        if (list.isEmpty()) {
            return ResultStruct.success(null);
        }
        HeatModel heatModel = new HeatModel();
        heatModel.setChartTitle("关联项目热力图");

        MenuAuthorityJ6InParam menuAuthorityJ6InParam = new MenuAuthorityJ6InParam();
        menuAuthorityJ6InParam.setProjectCode(list);
        menuAuthorityJ6InParam.setCreateTime(createTime);
        menuAuthorityJ6InParam.setProductCode(productId);
        final List<MenuAuthorityJ6Dto> menuAuthorityJ6Dtos = menuAuthorityService.selectMenuAuthorityByProjectIdAndCreateData(menuAuthorityJ6InParam);
        if (menuAuthorityJ6Dtos.isEmpty()) {
            return ResultStruct.success(heatModel);
        }

        List<String> abscissa = new ArrayList<>();
        List<String> ordinate = new ArrayList<>();
        List<HeatDataModel> dataList = new ArrayList<>(menuAuthorityJ6Dtos.size());
        heatModel.setAbscissa(abscissa);//横坐标：界面
        heatModel.setOrdinate(ordinate);//纵坐标：项目
        heatModel.setDataList(dataList);

        int absIndex = 0;
        int ordIndex = 0;

        for (MenuAuthorityJ6Dto menuAuthorityJ6Dto :
                menuAuthorityJ6Dtos) {
            final String projName = menuAuthorityJ6Dto.getProjName();
            final String menuName = menuAuthorityJ6Dto.getMenuName();
            final BigDecimal weightNum = menuAuthorityJ6Dto.getWeightNum();
            int x, y;
            if (abscissa.indexOf(menuName) < 0) {
                abscissa.add(menuName);
                absIndex++;
            }
            x = absIndex;

            if (ordinate.indexOf(projName) < 0) {
                ordinate.add(projName);
                ordIndex++;
            }
            y = ordIndex;
            HeatDataModel heatDataModel = new HeatDataModel();
            heatDataModel.setX(x);
            heatDataModel.setY(y);
            heatDataModel.setValue(weightNum);
            dataList.add(heatDataModel);
        }

        return ResultStruct.success(heatModel);
    }

    private List<ProjectD4Model> getProject20OrderBy() {
        return projectService.selectProjectD4OrderBy();
    }

    private List<String> get20IdOrderBy() {
        final List<ProjectD4Model> projectD4Models = getProject20OrderBy();
        if (!projectD4Models.isEmpty()) {
            List<String> list = new ArrayList<>(projectD4Models.size());
            for (ProjectD4Model item :
                    projectD4Models) {
                list.add(item.getId());
            }
            projectD4Models.clear();
            return list;
        } else {
            return null;
        }
    }

    /**
     * 展示端方法：J6，方法ID：SE2021072417135170476E17165E8C9
     *
     * @param productId  表menu_authority,字段名projectCode:项目CODE
     * @param createTime 表menu_authority,字段名createTime:数据生成时间
     * @return 界面加权平均值
     */
    @ApiOperation(value = "展示端方法：J6[界面加权平均值],方法ID：SE2021072417135170476E17165E8C9", notes = "/menuAuthorityController/V1/selectMenuAuthorityByProjectIdAndCreateData?projectCode=projectCode&createTime=createTime")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "createTime", value = "数据生成时间", dataType = "Date", required = true)
    })
    @PostMapping("/V1/selectMenuAuthorityByProjectIdAndCreateData")
    public ResultVO selectMenuAuthorityByProjectIdAndCreateData(@RequestParam(value = "productId") String productId
            , @RequestParam(value = "createTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime) {
        final List<String> list = projectAndProductService.selectProjectListByProductId(productId);
        if (!list.isEmpty()) {
            MenuAuthorityJ6InParam menuAuthorityJ6InParam = new MenuAuthorityJ6InParam();
            menuAuthorityJ6InParam.setProjectCode(list);
            menuAuthorityJ6InParam.setCreateTime(createTime);

            return ResultStruct.success(menuAuthorityService.selectMenuAuthorityByProjectIdAndCreateData(menuAuthorityJ6InParam));
        } else {
            return ResultStruct.success(null);
        }
    }

    /**
     * 展示端方法：J66，方法ID：SE2021072417135170476E17165E8C0
     *
     * @param projectId  表menu_authority,字段名projectCode:项目CODE
     * @param createTime 表menu_authority,字段名createTime:数据生成时间
     * @return 界面加权平均值
     */
    @ApiOperation(value = "展示端方法：J66[界面加权平均值],方法ID：SE2021072417135170476E17165E8C0", notes = "/menuAuthorityController/V1/selectMenuAuthorityByProjectIdAndCreateData?projectCode=projectCode&createTime=createTime")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "createTime", value = "数据生成时间", dataType = "Date", required = true)
    })
    @PostMapping("/V1/selectMenuAuthorityByProjectIdAndCreateDataForProjectId")
    public ResultVO selectMenuAuthorityByProjectIdAndCreateDataForProjectId(@RequestParam(value = "projectId") String projectId
            , @RequestParam(value = "createTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime) {
        final List<String> list = new ArrayList<>(1);
        list.add(projectId);
        MenuAuthorityJ6InParam menuAuthorityJ6InParam = new MenuAuthorityJ6InParam();
        menuAuthorityJ6InParam.setProjectCode(list);
        menuAuthorityJ6InParam.setCreateTime(createTime);
        final List<MenuAuthorityJ6Dto> menuAuthorityJ6Dtos = menuAuthorityService.selectMenuAuthorityByProjectIdAndCreateData(menuAuthorityJ6InParam);
        if (menuAuthorityJ6Dtos != null) {
            menuAuthorityJ6Dtos.forEach(a -> a.setId(LhClass.getDoorPictureKey(Short.valueOf(getPropertiesClass.getPort()))));
        }
        return ResultStruct.success(menuAuthorityJ6Dtos);
    }

    @ApiOperation(value = "得到历史次数列表与权重和,方法ID：SE202107221749238706FB5A8E67AYY", notes = "/menuAuthorityController/V1/selectHistoryCount?projectCode=projectCode&menuCode=menuCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目CODE", dataType = "String", required = true)
            , @ApiImplicitParam(name = "menuCode", value = "menuCode", dataType = "String", required = true)
    })
    @PostMapping("/V1/selectHistoryCount")
    public ResultVO selectHistoryCount(@RequestParam(value = "projectCode") String projectCode
            , @RequestParam(value = "menuCode") String menuCode) {
//        MenuHistoryModel menuHistoryModel = new MenuHistoryModel();
//        menuHistoryModel.setProjectCode(projectCode);
//        menuHistoryModel.setMenuCode(menuCode);
//
//        final BigDecimal bigDecimal = colService.selectColTotalWeightByProjectId(projectCode);
//        final List<MenuHistoryListModel> menuHistoryListModels = menuAuthorityService.selectHistoryCount(menuHistoryModel);
//        MenuHistoryTotalModel menuHistoryListModel = new MenuHistoryTotalModel();
//        menuHistoryListModel.setWeight(bigDecimal);
//        menuHistoryListModel.setMenuHistoryListModels(menuHistoryListModels);
        return ResultStruct.success(null);
    }


}